//index.js
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingHidden: true,
    isScroll: true,
    currentTabsIndex: 1,
    index: 1,
    num: 0,
    inputText: "",
    time: "",
    fishishTime: "",
    state: "",
    btn: "",
    orderList: [],
    finishList: [],
    id: 0,
    toView: "",
    scrollList: [],
    show: false,
    page: 1,
    title: "",
    receiveUserIdArray: []
  },
  /**
   * 抢单事件函数
   */
  // 完成的详情页
  // detailFin: function (e) {
  //   var orderId = e.currentTarget.dataset.finish;
  //   var receiveUserId = e.currentTarget.dataset.receiveuserid;
  //   console.log(receiveUserId);
  //   wx.navigateTo({
  //     url: '../finish/finish?userId=' + orderId + "&receiveUserId=" + receiveUserId
  //   })
  // },

  toOrder: function() {
    wx.switchTab({
      url: '../order/order',
    })
  },
  // 详情页
  detail: function(e) {
    var index = e.currentTarget.dataset.id;
    var status = e.currentTarget.dataset.status;
    var receiveUserIdArray = e.currentTarget.dataset.array;
    var publishId = e.currentTarget.dataset.publish;
    var receiveUserId = e.currentTarget.dataset.receiveuserid;
    console.log(receiveUserIdArray);
    var array = JSON.stringify(receiveUserIdArray)
    console.log(receiveUserId + "receiveUserId");
    console.log(publishId + "publishId")
    console.log(index);
    this.setData({
      receiveUserIdArray: receiveUserIdArray
    })
    var that = this;
    if (status <= 4 && that.data.receiveUserIdArray.length == 0) {
      wx.navigateTo({
        url: '../masterList/masterList?userId=' + publishId + "&receiveUserIdArray=" + array,
    })

    } else if (status <= 4 && that.data.receiveUserIdArray.length > 0) {
      wx.navigateTo({
        url: '../masterList/masterList?userId=' + publishId +"&receiveUserIdArray="+array,
      })
    } else if (status > 4) {
      wx.navigateTo({
        url: '../servering/servering?userId=' + index + "&receiveUserId=" + receiveUserId
      })
      wx.setStorageSync("orderDetailId", index)
    }
  
  },
  // 待处理订单
  order: function() {
    wx.redirectTo({
      url: '../dispose/dispose',
    })
  },
  //  下拉刷新
  nextPage: function() {
    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/serviceOrder/sendFindAll',
      method: "post",
      data: {
        userId: wx.getStorageSync("userId"),
        page: that.data.page,
        size: 10
      },
      success: function(res) {
        console.log(res);
        that.setData({
          page:that.data.page+1
        })
        console.log(that.data.orderList);
        var content = that.data.orderList.concat(res.data.data)
        that.setData({
          orderList: content,
        });

        console.log(that.data.orderList);
      }
    })
  },
  loadingChange(e){
    console.log(e);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
     wx.showLoading({
      title: '加载中',
    })
   this.firstPage();
    if (this.data.orderList.length == 0) {
  
    }
  },
  // 第一页
  firstPage(){
    var that=this;
    wx.request({
      url: 'https://www.xiaozheng8.com/serviceOrder/sendFindAll',
      method: "post",
      data: {
        userId: wx.getStorageSync("userId"),
        page: 0,
        size: 10
      },
      success: function (res) {
        console.log(res);
        that.setData({
          page: 0
        })
      
        // console.log(that.data.orderList);
        var content = res.data.data;
        that.setData({
          orderList: content,
        });
        setTimeout(function () {
          wx.hideLoading();
        }, 500);
      }
    })
  },
  // 去下单
  btn: function() {
    wx.reLaunch({
      url: '../order/order',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var that = this;

  },
  request: function() {
    var that = this;
    wx.request({
        url: 'https://www.xiaozheng8.com/serviceOrder/sendFindAll',
        method: "post",
        data: {
          userId: wx.getStorageSync("userId")
        },
        success: function(res) {
          console.log(res.data);
          // that.setData({
          //   orderList: res.data.data,
          // });
          wx.hideToast();
          if(res.data==""){
            return false;
          }
          // console.log(that.data.project);
          var title = "";
          for (var i = 0; i < res.data.data.length; i++) {
            
            if (res.data.data[i].isCancel == true) {
              res.data.data.splice(i, 1);
           
            }
          }
         
        
          var length = [];
          console.log(res.data.data.length);
          if (res.data.data.length==0){
            that.setData({
              show:true
            })
          }
          for (var i = 0; i < res.data.data.length; i++) {
            if ((res.data.data[i].receiveUserIdArray.length > 0 && res.data.data[i].status == 4) || res.data.data[i].status == 10) {
              length.push(i);
              console.log(i);
              var id = res.data.data[i].sendUserId;
              console.log(id)
              that.setData({
                id: id
              })

            }
          }
          that.setData({
            num: length.length
          })
          console.log(length);
          console.log(that.data.num);
         

        
        },fail(err){
          wx.hideToast();
          return false;
        }
      }),
    
      this.animation = wx.createAnimation();
  },

  inputText: function(e) {
    let that = this;
    // console.log(e);
    that.setData({
      inputText: e.target.dataset.name
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.request();
    wx.removeTabBarBadge({
      index: 1,
    });
    // app.request();
    // this.setData({
    //   num:wx.getStorageSync("tabBarNum")
    // });
    this.firstPage();
  },

  text: function(e) {
    let that = this;
    // console.log(e);
    that.setData({
      text: e.target.dataset.text
    })
  },
  itemTap: function(e) {
    var index = e.currentTarget.dataset.index;
    console.log(index);
    this.setData({
      currentTabsIndex: index
    })
  },
  // 订单跳转
  order: function() {
    var that = this;
    this.setData({
      toView: that.data.id
    })
  },
  // 禁止苹果下拉测试
  onPageScroll: function(e) {
    if (e.scrollTop < 0) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.nextPage();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  onPullDownRefresh: function() {
      
   var that=this;
    wx.startPullDownRefresh();
    that.firstPage();
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 2000)
    
  },
})